package io.bsly.modules.app.controller;

import io.bsly.common.utils.Constant;
import io.bsly.common.utils.PageUtils;
import io.bsly.common.utils.R;
import io.bsly.modules.app.entity.localEntity.DyOrderEntity;
import io.bsly.modules.app.pojo.OrderSubmitVo;
import io.bsly.modules.app.service.localmaster.DyOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


/**
 * 用户订单
 *
 * @author fys
 * @email demo@gmail.com
 * @date 2021-09-27 10:00:13
 */
@Api(tags = "app订单模块")
@Slf4j
@RestController
@RequestMapping("app/dyorder")
public class DyOrderController {
    @Autowired
    private DyOrderService dyOrderService;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 列表
     */
    @ApiOperation("分页展示")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "orderId",value = "订单id" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "productCode",value = "产品编码" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "hotelName",value = "酒店名称" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "inDate",value = "入住日期" , dataType = "string", paramType = "query",example = "2021-09-28"),
            @ApiImplicitParam(name = "outDate",value = "离店日期" , dataType = "string", paramType = "query",example = "2021-09-28"),
            @ApiImplicitParam(name = "orderTime",value = "订单时间" , dataType = "string", paramType = "query",example = "2021-09-29 11:33:40"),
            @ApiImplicitParam(name = "dyCouponCode",value = "核销码" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "dyOrderCode",value = "抖音平台订单号码" , dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "status",value = "状态,多个用逗号隔开" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page",value = "当前页数" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "每页条数" , dataType = "int", paramType = "query")
    })
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dyOrderService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("根据id查询")
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		DyOrderEntity dyOrder = dyOrderService.getById(id);
        return R.ok().put("dyOrder", dyOrder);
    }

    /**
     * 获取订单token
     */
    @ApiOperation("获取下单的token，30分钟有效")
    @GetMapping("/orderToken/{userId}")
    public R getOrderToken(@PathVariable("userId") String userId){
        String token = UUID.randomUUID().toString().replace("-", "");
        redisTemplate.opsForValue().set(Constant.USER_ORDER_TOKEN_PREFIX+userId,token,30, TimeUnit.MINUTES);
        return R.ok().put("orderToken",token);
    }
    /**
     * 创建订单
     */
    @ApiOperation("下单")
    @PostMapping("/save")
    public R save(@RequestBody @Valid OrderSubmitVo orderVo, HttpServletRequest request){
        return  dyOrderService.saveOrder(orderVo,request);
    }

    /**
     * 取消订单
     */
    @ApiOperation("根据订单IDq取消订单")
    @PostMapping("/cancelOrder")
    public R cancelOrder(@RequestBody String orderId){
        dyOrderService.cancelOrder(orderId);
        return R.ok();
    }




    /**
     * 修改
     */
    @ApiOperation("根据订单IDq修改订单")
    @PostMapping("/update")
    public R update(@RequestBody DyOrderEntity dyOrder){
		dyOrderService.updateById(dyOrder);
        return R.ok();
    }

    /**
     * 删除  ["DY202109300027"]
     */
    @ApiOperation("根据id数组删除")
    @DeleteMapping("/delete")
    public R delete(@RequestBody String[] ids){
		dyOrderService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
